
/*
Data on benefits, no results using these data in the accepted version of the paper
*/

* Keep only the needed variables to save memory, merge the others back later
use "$data\01_main\folk_flown_merge.dta", clear
drop syrtun_ownrshp syrtun tol // drop the firm and industry info
preserve
keep shnro vuosi sukup ikasq pks highed married lkm_k mainowner 
save "$data\99_temp\temp_folk_flown_merge_othervars.dta", replace // merged back later
restore
keep shnro id vuosi dspi mkti mkti_d_are_1 mkti_d_are_2 has_d_are ie uie new_ie new_uie ika new_firm

preserve
use vuosi shnro tyotu elatulo tkotihtu aiprva tyotur astuki opira toimtu if vuosi>=1995 using "D:\ready-made\FOLK_tulo_8800a\folk_19872000_tua_tulo21tot_1.dta", clear
append using "D:\ready-made\FOLK_tulo_0110a\folk_20012010_tua_tulo21tot_1.dta", keep(vuosi shnro tyotu elatulo tkotihtu aiprva tyotur astuki opira toimtu)
append using "D:\ready-made\FOLK_tulo_11a\folk_20112019_tua_tulo21tot_1.dta", keep(vuosi shnro tyotu elatulo tkotihtu aiprva tyotur astuki opira toimtu)
foreach var of varlist tyotu elatulo tkotihtu aiprva tyotur astuki opira toimtu {
	replace `var'=0 if `var'==.
}
gen benef_nom = elatulo + tkotihtu + aiprva + tyotur + astuki + opira + toimtu
rename tyotu tyotu_nom
keep shnro vuosi tyotu_nom benef_nom
save "$data\99_temp\temp_benef.dta", replace
restore
merge 1:1 shnro vuosi using "$data\99_temp\temp_benef.dta"
keep if _merge==3
drop _merge
erase "$data\99_temp\temp_benef.dta"
gen tyotu=.
gen benef=.
foreach var of varlist tyotu benef {
	replace `var' = `var'_nom / 0.8680 if vuosi==1995
	replace `var' = `var'_nom / 0.8730 if vuosi==1996
	replace `var' = `var'_nom / 0.8840 if vuosi==1997
	replace `var' = `var'_nom / 0.8960 if vuosi==1998
	replace `var' = `var'_nom / 0.9070 if vuosi==1999
	replace `var' = `var'_nom / 0.9340 if vuosi==2000
	replace `var' = `var'_nom / 0.9590 if vuosi==2001
	replace `var' = `var'_nom / 0.9780 if vuosi==2002
	replace `var' = `var'_nom / 0.9910 if vuosi==2003
	replace `var' = `var'_nom / 0.9920 if vuosi==2004
	replace `var' = `var'_nom if vuosi==2005
	replace `var' = `var'_nom / 1.0130 if vuosi==2006
	replace `var' = `var'_nom / 1.0290 if vuosi==2007
	replace `var' = `var'_nom / 1.0690 if vuosi==2008
	replace `var' = `var'_nom / 1.0870 if vuosi==2009
	replace `var' = `var'_nom / 1.1050 if vuosi==2010
	replace `var' = `var'_nom / 1.1420 if vuosi==2011
	replace `var' = `var'_nom / 1.1780 if vuosi==2012
	replace `var' = `var'_nom / 1.2037 if vuosi==2013
	replace `var' = `var'_nom / 1.2183 if vuosi==2014
	replace `var' = `var'_nom / 1.2164 if vuosi==2015
	replace `var' = `var'_nom / 1.2211 if vuosi==2016
	replace `var' = `var'_nom / 1.2313 if vuosi==2017
	replace `var' = `var'_nom / 1.2457 if vuosi==2018
	replace `var' = `var'_nom / 1.2599 if vuosi==2019
	*replace `var' = `var'_nom / 1.2647 if vuosi==2020
}
gen mkti_and_benef=mkti+benef
gen shr_linc = tyotu / mkti_and_benef
keep shnro id vuosi dspi mkti mkti_d_are_1 mkti_d_are_2 has_d_are ie uie new_ie new_uie ika new_firm shr_linc
replace shr_linc=1 if shr_linc>1
replace shr_linc=0 if shr_linc<0
sum shr_linc if ie==0 & uie==0 & dspi>0 & mkti>0 & ika>24 & ika<65, d
label var shr_linc "Share of salaried earnings of market income"
/*preserve
keep if ie==0 & uie==0 & dspi>0 & mkti>0 & ika>24 & ika<65
twoway kdensity shr_linc, /*bwidth(0.01)*/ color(maroon%75) recast(area) graphr(c(white)) ysize(3) title("Labor share for prime-age non-entrepreneurs", size(medium)) legend(off) ytitle("Density") xtitle("Share") ylabel(, angle(0)) /*ysc(range(0 1)) ylabel(0(0.2)1)*/ xsc(range(0 1)) xlabel(0(0.2)1) name(shr_linc_we, replace)
restore*/

/*
We now have the panel of individuals for whom we have info on parental income
--> next get the starts, pseudo-starts and timelines properly defined for the analysis
*/

save "$data\01_main\folk_flown_merge.dta", replace









********************************************************************************
/* ***** Draw pseudo-starts for those who do not start a business ***** */

preserve
foreach var of varlist ie uie new_firm new_uie { // drop if you ever were an entrepreneur
	bysort shnro: egen `var'_tot = total(`var')
}
drop if ie_tot>0 | uie_tot>0 | new_firm_tot>0 | new_uie_tot>0
drop ie_tot uie_tot new_firm_tot new_uie_tot
/*bysort id: gen nobs = _N // drop those who we observe less than 17 times before any age / income restrictions, consecutively observed
drop if nobs<17
xtset id vuosi, yearly
gen d_vuosi = vuosi - L.vuosi
bysort shnro: gen nr = _n
replace d_vuosi=1 if nr==1
drop nr
bysort shnro: gen missing=1 if d_vuosi!=1
replace missing=0 if missing==.
bysort shnro: egen missyoumuch=max(missing)
drop if missyoumuch==1
drop nobs d_vuosi missing missyoumuch*/
keep shnro id vuosi ika dspi mkti shr_linc
set seed 76850
bysort id: gen psdstrt_temp1 = runiform() // draw numbers randomly for an individual
bysort id: egen psdstrt_temp2 = max(psdstrt_temp1) // collect the max of drawn numbers
gen psdstrt_temp3 = psdstrt_temp1-psdstrt_temp2 // this line and the following two leave one random '1' per individual
gen psdstrt=0
replace psdstrt=1 if psdstrt_temp3==0
drop if vuosi>=2015
keep if ika>24 & ika<66
drop if dspi==0
*drop if mkti==0
*drop if shr_linc<0.5 // drop those whose labor income share is below 50%: this will mess up the trajectories for pseudo-starters
sort shnro vuosi
keep shnro vuosi psdstrt
save "$data\99_temp\temp_draws.dta", replace
restore

merge 1:1 shnro vuosi using "$data\99_temp\temp_draws.dta"
drop _merge
erase "$data\99_temp\temp_draws.dta"

keep if ika>19 & ika<76
drop if dspi<=0
drop if mkti<=0

/*foreach var of varlist ie uie new_firm new_uie { // make sure that a person is not in many groups
	bysort shnro: egen `var'_tot = total(`var')
}
replace psdstrt=0 if ie_tot>0
replace psdstrt=0 if uie_tot>0
replace psdstrt=0 if new_firm_tot>0
replace psdstrt=0 if new_uie_tot>0
replace psdstrt=0 if psdstrt==.
drop ie_tot uie_tot new_firm_tot new_uie_tot*/

* Because the information on NEW unincorporated entrepreneurs start from 1998:
replace new_firm=0 if vuosi<=1997
replace new_uie=0 if vuosi<=1997
replace psdstrt=0 if vuosi<=1997

* Rename variables
rename mkti_d_are_1 mkti_adj1
rename mkti_d_are_2 mkti_adj2

********************************************************************************
/* ***** Define the event variables ***** */

gen event_ie=. // event for incorporated entrepreneurs
replace new_firm=0 if vuosi>=2015
replace new_firm=0 if ika<25 | ika>65
xtset id vuosi, yearly
replace event_ie=-5 if F5.new_firm==1
replace event_ie=-4 if F4.new_firm==1
replace event_ie=-3 if F3.new_firm==1
replace event_ie=-2 if F2.new_firm==1
replace event_ie=-1 if F.new_firm==1
replace event_ie=0 if new_firm==1
replace event_ie=1 if L.new_firm==1
replace event_ie=2 if L2.new_firm==1
replace event_ie=3 if L3.new_firm==1
replace event_ie=4 if L4.new_firm==1
replace event_ie=5 if L5.new_firm==1
replace event_ie=6 if L6.new_firm==1
replace event_ie=7 if L7.new_firm==1
replace event_ie=8 if L8.new_firm==1
replace event_ie=9 if L9.new_firm==1
replace event_ie=10 if L10.new_firm==1

gen event_uie=. // event for unincorporated entrepreneurs
replace new_uie=0 if vuosi>=2015
replace new_uie=0 if ika<25 | ika>65
xtset id vuosi, yearly
replace event_uie=-5 if F5.new_uie==1
replace event_uie=-4 if F4.new_uie==1
replace event_uie=-3 if F3.new_uie==1
replace event_uie=-2 if F2.new_uie==1
replace event_uie=-1 if F.new_uie==1
replace event_uie=0 if new_uie==1
replace event_uie=1 if L.new_uie==1
replace event_uie=2 if L2.new_uie==1
replace event_uie=3 if L3.new_uie==1
replace event_uie=4 if L4.new_uie==1
replace event_uie=5 if L5.new_uie==1
replace event_uie=6 if L6.new_uie==1
replace event_uie=7 if L7.new_uie==1
replace event_uie=8 if L8.new_uie==1
replace event_uie=9 if L9.new_uie==1
replace event_uie=10 if L10.new_uie==1

gen event_we=. // event for pseudo-starters
xtset id vuosi, yearly
replace event_we=-5 if F5.psdstrt==1
replace event_we=-4 if F4.psdstrt==1
replace event_we=-3 if F3.psdstrt==1
replace event_we=-2 if F2.psdstrt==1
replace event_we=-1 if F.psdstrt==1
replace event_we=0 if psdstrt==1
replace event_we=1 if L.psdstrt==1
replace event_we=2 if L2.psdstrt==1
replace event_we=3 if L3.psdstrt==1
replace event_we=4 if L4.psdstrt==1
replace event_we=5 if L5.psdstrt==1
replace event_we=6 if L6.psdstrt==1
replace event_we=7 if L7.psdstrt==1
replace event_we=8 if L8.psdstrt==1
replace event_we=9 if L9.psdstrt==1
replace event_we=10 if L10.psdstrt==1

drop if event_ie==. & event_uie==. & event_we==.

* Examine how many years we observe the individuals
preserve
keep if event_ie!=.
tab vuosi
bysort shnro: gen nobs_arnd_evnt = _N
collapse nobs_arnd_evnt, by(shnro)
tab nobs_arnd_evnt
restore
preserve
keep if event_uie!=.
tab vuosi
bysort shnro: gen nobs_arnd_evnt = _N
collapse nobs_arnd_evnt, by(shnro)
tab nobs_arnd_evnt
restore
preserve
keep if event_we!=.
tab vuosi
bysort shnro: gen nobs_arnd_evnt = _N
collapse nobs_arnd_evnt, by(shnro)
tab nobs_arnd_evnt
restore









********************************************************************************
/* ***** Calculate income ranks and define some variables ***** */

* Disposable income
forvalues i = 1995(1)2019{
	fastxtile ptile = dspi if vuosi==`i' /*& ika>=25 & ika<=65*/, nq(100)
	rename ptile ptile_`i'
}
gen ptile_dspi=.
forvalues i = 1995(1)2019{
	replace ptile_dspi=ptile_`i' if vuosi==`i' //& ika>=25 & ika<=65
}
drop ptile_1995-ptile_2019

* Market income
forvalues i = 1995(1)2019{
	fastxtile ptile = mkti if vuosi==`i' /*& ika>=25 & ika<=65*/, nq(100)
	rename ptile ptile_`i'
}
gen ptile_mkti=.
forvalues i = 1995(1)2019{
	replace ptile_mkti=ptile_`i' if vuosi==`i' //& ika>=25 & ika<=65
}
drop ptile_1995-ptile_2019

* Market income, adjusted by profits-dividends
forvalues i = 1995(1)2019{
	fastxtile ptile = mkti_adj1 if vuosi==`i' /*& ika>=25 & ika<=65*/, nq(100)
	rename ptile ptile_`i'
}
gen ptile_mkti_adj1=.
forvalues i = 1995(1)2019{
	replace ptile_mkti_adj1=ptile_`i' if vuosi==`i' //& ika>=25 & ika<=65
}
drop ptile_1995-ptile_2019

* Market income, adjusted by profits-dividends-investments
forvalues i = 1995(1)2019{
	fastxtile ptile = mkti_adj2 if vuosi==`i' /*& ika>=25 & ika<=65*/, nq(100)
	rename ptile ptile_`i'
}
gen ptile_mkti_adj2=.
forvalues i = 1995(1)2019{
	replace ptile_mkti_adj2=ptile_`i' if vuosi==`i' //& ika>=25 & ika<=65
}
drop ptile_1995-ptile_2019

gen log_dspi=log(dspi)
gen log_mkti=log(mkti)
gen log_mkti_adj1=log(mkti_adj1)
gen log_mkti_adj2=log(mkti_adj2)
order shnro-mkti log_dspi log_mkti log_mkti_adj1 log_mkti_adj2 ptile_dspi ptile_mkti ptile_mkti_adj1 ptile_mkti_adj2 event_ie event_uie event_we










********************************************************************************
/* ***** Merge the main data back ***** */

merge 1:1 shnro vuosi using "$data\99_temp\temp_folk_flown_merge_othervars.dta", keep(3)
drop _merge
erase "$data\99_temp\temp_folk_flown_merge_othervars.dta"
merge m:1 shnro using "$data\02_parents\ptile_parent.dta", keep(3) keepusing(ptile)
drop _merge
rename ptile ptile_prnt
label var ptile_prnt "Parental income rank (both parents, 46-50 yo)"
erase "$data\02_parents\ptile_parent.dta" // the other two files in 02_parents are used later

********************************************************************************
/* ***** Fix the controls at t-1 ***** */

preserve
keep if event_ie==-1 | event_uie==-1 | event_we==-1
keep shnro log_dspi log_mkti log_mkti_adj1 log_mkti_adj2 ika sukup pks highed married
foreach var of varlist log_dspi log_mkti log_mkti_adj1 log_mkti_adj2 ika sukup pks highed married {
	rename `var' `var'_tmin1
}
duplicates drop shnro, force
save "$data\99_temp\folk_flown_merge_eventready_allentrep_min1info.dta", replace
restore

merge m:1 shnro using "$data\99_temp\folk_flown_merge_eventready_allentrep_min1info.dta"
drop if _merge==1
drop _merge
erase "$data\99_temp\folk_flown_merge_eventready_allentrep_min1info.dta"

********************************************************************************
/* ***** Fix the rank as an average between t-5 and t-1 ***** */

preserve
drop if event_ie>-1 & event_ie<=10
drop if event_uie>-1 & event_uie<=10
drop if event_we>-1 & event_we<=10
keep shnro ptile_dspi ptile_mkti ptile_mkti_adj1 ptile_mkti_adj2
collapse ptile_dspi ptile_mkti ptile_mkti_adj1 ptile_mkti_adj2, by(shnro)
rename ptile_dspi ptile_dspi_tmin1
rename ptile_mkti ptile_mkti_tmin1
rename ptile_mkti_adj1 ptile_mkti_adj1_tmin1
rename ptile_mkti_adj2 ptile_mkti_adj2_tmin1
duplicates drop shnro, force
save "$data\99_temp\folk_flown_merge_eventready_allentrep_rankinfo.dta", replace
restore

merge m:1 shnro using "$data\99_temp\folk_flown_merge_eventready_allentrep_rankinfo.dta"
drop if _merge==1
drop _merge
erase "$data\99_temp\folk_flown_merge_eventready_allentrep_rankinfo.dta"

replace ptile_dspi_tmin1=round(ptile_dspi_tmin1, 1)
replace ptile_mkti_tmin1=round(ptile_mkti_tmin1, 1)
replace ptile_mkti_adj1_tmin1=round(ptile_mkti_adj1_tmin1, 1)
replace ptile_mkti_adj2_tmin1=round(ptile_mkti_adj2_tmin1, 1)
order shnro-mkti mkti_adj1 mkti_adj2

********************************************************************************
/* ***** Merge data on benefits ***** */

preserve
use vuosi shnro vuosi shnro elatulo tkotihtu aiprva tyotur astuki opira toimtu if vuosi>=1995 using "D:\ready-made\FOLK_tulo_8800a\folk_19872000_tua_tulo20tot_1.dta", clear
append using "D:\ready-made\FOLK_tulo_0110a\folk_20012010_tua_tulo20tot_1.dta", keep(vuosi shnro vuosi shnro elatulo tkotihtu aiprva tyotur astuki opira toimtu)
append using "D:\ready-made\FOLK_tulo_11a\folk_20112019_tua_tulo21tot_1.dta", keep(vuosi shnro vuosi shnro elatulo tkotihtu aiprva tyotur astuki opira toimtu)
save "$data\99_temp\temp_benefits.dta", replace
restore

merge 1:1 shnro vuosi using "$data\99_temp\temp_benefits.dta"
keep if _merge==3
drop _merge
erase "$data\99_temp\temp_benefits.dta"

foreach var of varlist elatulo tkotihtu aiprva tyotur astuki opira toimtu {
	replace `var'=0 if `var'==.
	gen `var'_dummy=0
	replace `var'_dummy=1 if `var'>0
}
gen nobenefits_dummy=0
replace nobenefits_dummy=1 if elatulo_dummy==0 & tkotihtu_dummy==0 & aiprva_dummy==0 & tyotur_dummy==0 & astuki_dummy==0 & opira_dummy==0 & toimtu_dummy==0

save "$data\01_main\folk_flown_merge_eventready.dta", replace
clear

********************************************************************************
/* ***** Pre-entrepreneurship industry & info on whether parents were entrepreneurs ***** */

/*
Industry prior to becoming an entrepreneur
*/

use vuosi shnro tvm toimiala if vuosi>=1995 using "D:\ready-made\FOLK_jaksot_a\folk_tyosuhde_19872000_1.dta", clear
append using "D:\ready-made\FOLK_jaksot_a\folk_tyosuhde_20012010_1.dta", keep(vuosi shnro tvm toimiala)
append using "D:\ready-made\FOLK_jaksot_a\folk_tyosuhde_20112019_1.dta", keep(vuosi shnro tvm toimiala)
keep if tvm==1
drop tvm
save "$data\99_temp\employment_tags.dta", replace
use "$data\01_main\folk_flown_merge_eventready.dta", clear
merge 1:1 shnro vuosi using "$data\99_temp\employment_tags.dta"
erase "$data\99_temp\employment_tags.dta"
drop if _merge==2
drop _merge
gen tol2dig = substr(toimiala, 1, 2)
replace tol2dig="" if tol2dig=="DN" | tol2dig=="N" | tol2dig=="O" | tol2dig=="R"
replace tol2dig="100" if tol2dig==""
destring tol2dig, replace
label var tol2dig "Industry 08 as a wage earner (100=missing)"
gen uie_to_ie=0
replace uie_to_ie=1 if event_ie==-1 & uie==1
save "$data\01_main\folk_flown_merge_eventready.dta", replace

/*
Whether parents were entrepreneurs
- if socioeconomic status was entrepreneur for at least five years
	- this captures pretty much all
- parent was an entrepreneur if he/she ever earned over 1,000 EUR in entrepreneurial income in any year
	- we exclude those cases where entrepreneurial income equals tmaat1 (farm income)
*/

* Info on whether received entrepreneurial income
use vuosi shnro tyrtuo using "D:\ready-made\FOLK_tulo_8800a\folk_19872000_tua_tulo20tot_1.dta", clear
append using "D:\ready-made\FOLK_tulo_0110a\folk_20012010_tua_tulo20tot_1.dta", keep(vuosi shnro tyrtuo tmaat1)
append using "D:\ready-made\FOLK_tulo_11a\folk_20112019_tua_tulo21tot_1.dta", keep(vuosi shnro tyrtuo tmaat1)
replace tyrtuo=0 if tyrtuo==tmaat1
save "$data\99_temp\tyrtuo_prnt_temp.dta", replace

* Info on whether sose==20 (if you were an entrepreneur by variable socioeconomic status)
use shnro sose using "D:\ready-made\FOLK_perus_8800a\folk_19872000_tua_perus20tot_2.dta", clear
append using "D:\ready-made\FOLK_perus_0110a\folk_20012010_tua_perus20tot_2.dta", keep(vuosi shnro sose)
append using "D:\ready-made\FOLK_perus_11a\folk_20112019_tua_perus20tot_2.dta", keep(vuosi shnro sose)
keep if sose=="20"
bysort shnro: gen nobs=_N
keep if nobs>=5
bysort shnro: gen nr=_n
keep if nr==1
gen sose_e=1
keep shnro sose_e
save "$data\99_temp\sose_prnt_temp.dta", replace

* Info on age
use vuosi shnro ika using "D:\ready-made\FOLK_perus_8800a\folk_19872000_tua_perus20tot_2.dta", clear
append using "D:\ready-made\FOLK_perus_0110a\folk_20012010_tua_perus20tot_2.dta", keep(vuosi shnro ika)
append using "D:\ready-made\FOLK_perus_11a\folk_20112019_tua_perus20tot_2.dta", keep(vuosi shnro ika)
save "$data\99_temp\Age_folk_1987_2019.dta", replace

* Daddy links
use "D:\ready-made\FOLK_laps_70a\folk_19702020_tua_laps21_1.dta", clear
* drop if daddy and mommy are missing
drop if shnro_m=="" & shnro_f==""
drop folk*
* drop if dude dies before 2002
drop if kuolv<=2002
sort syntyv
keep if shnro_f!=""
ren shnro shnro_kid
ren shnro_f shnro
drop shnro_m
gen dad=1
save "$data\99_temp\kids_dads.dta", replace

* Mommy links
use "D:\ready-made\FOLK_laps_70a\folk_19702020_tua_laps21_1.dta", clear
* drop if daddy and mommy are missing
drop if shnro_m=="" & shnro_f==""
drop folk*
* drop if dude dies before 2002
drop if kuolv<=2002
sort syntyv
keep if shnro_m!=""
ren shnro shnro_kid
ren shnro_m shnro
drop shnro_f
gen mom=1
save "$data\99_temp\kids_moms.dta", replace

* Combine both parents
use "$data\99_temp\kids_dads.dta", clear
append using "$data\99_temp\kids_moms.dta"
sort shnro_kid syntyv
save "$data\99_temp\kids_parents.dta", replace

* Keep only "unique parents"
use "$data\99_temp\kids_parents.dta", clear
keep if dad==1
duplicates drop shnro, force
save "$data\99_temp\kids_dads_uniq.dta", replace
use "$data\99_temp\kids_parents.dta", clear
keep if mom==1
duplicates drop shnro, force
save "$data\99_temp\kids_moms_uniq.dta", replace

* Narrow down to parents when they are 26-50 yo
use "$data\99_temp\tyrtuo_prnt_temp.dta", clear
merge 1:1 shnro vuosi using "$data\99_temp\Age_folk_1987_2019.dta"
drop _merge
keep if ika>25 & ika<=50
merge m:1 shnro using "$data\99_temp\kids_dads_uniq.dta"
keep if dad==1
save "$data\99_temp\tyrtuo_kids_dads.dta", replace
use "$data\99_temp\tyrtuo_prnt_temp.dta", clear
merge 1:1 shnro vuosi using "$data\99_temp\Age_folk_1987_2019.dta"
drop _merge
keep if ika>25 & ika<=50
merge m:1 shnro using "$data\99_temp\kids_moms_uniq.dta"
keep if mom==1
save "$data\99_temp\tyrtuo_kids_moms.dta", replace
use "$data\99_temp\tyrtuo_kids_dads.dta", clear
append using "$data\99_temp\tyrtuo_kids_moms.dta"
drop if vuosi==.
keep shnro* syntyv kuolv dad mom tyrtuo
duplicates drop shnro, force
save "$data\99_temp\tyrtuo_kids_dads_moms.dta", replace

*Combine all of the above
use "$data\99_temp\kids_parents.dta", clear
duplicates drop shnro shnro_kid, force
merge m:m shnro using "$data\99_temp\tyrtuo_kids_dads_moms.dta"
keep if _merge==3
drop _merge
sort shnro_kid 
bys shnro_kid: egen count=count(syntyv)
gen only_dad=1 if count==1 & dad==1
gen only_mom=1 if count==1 & mom==1
replace tyrtuo=0 if tyrtuo==.

* Tag mom / dad entrepreneurs
foreach var of varlist dad mom {
	gen `var'_entr=0
	replace `var'_entr=1 if tyrtuo>1000 & `var'==1
}
merge m:1 shnro using "$data\99_temp\sose_prnt_temp.dta"
drop if _merge==2
foreach var of varlist dad mom {
	replace `var'_entr=1 if `var'_entr==0 & sose_e==1 & `var'==1
}
rename dad_entr dad_entr0
rename mom_entr mom_entr0
bysort shnro_kid: egen dad_entr = max(dad_entr0)
bysort shnro_kid: egen mom_entr = max(mom_entr0)
collapse dad_entr mom_entr, by(shnro_kid)
rename shnro_kid shnro

* Save and merge with the main data
erase "$data\99_temp\tyrtuo_kids_dads_moms.dta"
erase "$data\99_temp\tyrtuo_kids_moms.dta"
erase "$data\99_temp\tyrtuo_kids_dads.dta"
erase "$data\99_temp\kids_moms_uniq.dta"
erase "$data\99_temp\kids_dads_uniq.dta"
erase "$data\99_temp\kids_parents.dta"
erase "$data\99_temp\kids_moms.dta"
erase "$data\99_temp\kids_dads.dta
erase "$data\99_temp\Age_folk_1987_2019.dta"
erase "$data\99_temp\sose_prnt_temp.dta"
erase "$data\99_temp\tyrtuo_prnt_temp.dta"
save "$data\99_temp\\tags_mom_dad_entrep.dta", replace

use "$data\01_main\folk_flown_merge_eventready.dta", clear
merge m:1 shnro using "$data\99_temp\tags_mom_dad_entrep.dta", keep(3)
drop _merge
erase "$data\99_temp\tags_mom_dad_entrep.dta"

********************************************************************************
/* ***** Final touches ***** */

order shnro - ptile_mkti_adj2 log_dspi_tmin1 log_mkti_tmin1 log_mkti_adj1_tmin1 log_mkti_adj2_tmin1 ptile_dspi_tmin1 ptile_mkti_tmin1 ptile_mkti_adj1_tmin1 ptile_mkti_adj2_tmin1 ika_tmin1 sukup_tmin1 pks_tmin1 highed_tmin1 married_tmin1
order shnro id vuosi ie new_ie new_firm mainowner uie new_uie psdstrt event_ie event_uie event_we
order shnro - ptile_mkti_adj2_tmin1 has_d_are shr_linc ika ikasq pks highed married sukup
order ptile_prnt dad_entr mom_entr, last
gen prnt_entr=0
replace prnt_entr=1 if dad_entr==1 | mom_entr==1

label var id "Person id, numerical"
label var psdstrt "Pseudo-start for those who do not transition into entrepreneurship"
label var event_ie "Event timeline for incorporated entrepreneurs"
label var event_uie "Event timeline for unincorporated entrepreneurs"
label var event_we "Event timeline for pseudo-starters"
label var log_dspi "Log disposable income"
label var log_mkti "Log market income"
label var log_mkti_adj1 "Log market income, adjusted (profits-dividends)"
label var log_mkti_adj2 "Log market income, adjusted (profits-dividends-investments)"
label var ptile_dspi "Percentile rank of disposable income"
label var ptile_mkti "Percentile rank of market income"
label var ptile_mkti_adj1 "Percentile rank of market income, adjusted (profits-dividends)"
label var ptile_mkti_adj2 "Percentile rank of market income, adjusted (profits-dividends-investments)"
label var log_dspi_tmin1 "Log disposable income at event time -1"
label var log_mkti_tmin1 "Log market income at event time -1"
label var log_mkti_adj1_tmin1 "Log market income, adjusted (profits-dividends), at event time -1"
label var log_mkti_adj2_tmin1 "Log market income, adjusted (profits-dividends-investments), at event time -1"
label var ptile_dspi_tmin1 "Average disposable income rank (-5, -1)"
label var ptile_mkti_tmin1 "Average market income rank (-5, -1)"
label var ptile_mkti_adj1_tmin1 "Average market income, adjusted (profits-dividends), rank (-5, -1)"
label var ptile_mkti_adj2_tmin1 "Average market income, adjusted (profits-dividends-investments), rank (-5, -1)"
label var elatulo "Pensions"
label var tkotihtu "Child care allowance"
label var aiprva "Parental leave allowance"
label var tyotur "Unemployment benefits"
label var astuki "Housing allowance"
label var opira "Student benefits"
label var toimtu "Income support"
label var elatulo_dummy "1 if received any pensions"
label var tkotihtu_dummy "1 if received any child care allowance"
label var aiprva_dummy "1 if received any parental leave allowance"
label var tyotur_dummy "1 if received any unemployment benefits"
label var astuki_dummy "1 if received any housing allowance"
label var opira_dummy "1 if received any student benefits"
label var toimtu_dummy "1 if received any income support"
label var nobenefits_dummy "1 if received none of the benefits"
label var toimiala "Industry as an entrepreneur or salaried worker, 5 digit"
label var tol2dig "Industry as an entrepreneur or salaried worker, 2 digit"
label var uie_to_ie "Transitioned from unincorporated to incorporated entrepreneurship"
label var dad_entr "Father entrepreneur"
label var mom_entr "Mother entrepreneur"
label var prnt_entr "Either parent entrepreneur"

save "$data\01_main\folk_flown_merge_eventready.dta", replace
